﻿using DoNet.Bpmn.Core.Enums;
using DoNet.Bpmn.Core.Vo;
using DoNet.Bpmn.Models;
using DoNet.Bpmn.Services;
using Microsoft.Extensions.Logging;

namespace DoNet.Bpmn.Adaptor.BpmnNode
{
    /// <summary>
    /// NodePropertyHrbpAdp Class for HRBP Node Properties
    /// </summary>
    public class NodePropertyHrbpAdaptor : BpmnNodeAdaptor
    {
        private readonly BpmnNodeHrbpConfigService _bpmnNodeHrbpConfigService;
        private readonly ILogger<NodePropertyHrbpAdaptor> _logger;

        public NodePropertyHrbpAdaptor(BpmnNodeHrbpConfigService bpmnNodeHrbpConfService, ILogger<NodePropertyHrbpAdaptor> logger)
        {
            _bpmnNodeHrbpConfigService = bpmnNodeHrbpConfService;
            _logger = logger;
        }

        public override BpmnNodeVo FormatToBpmnNodeVo(BpmnNodeVo bpmnNodeVo)
        {
            var bpmnNodeHrbpConf = _bpmnNodeHrbpConfigService.GetByBpmnNodeId(bpmnNodeVo.Id);

            if (bpmnNodeHrbpConf != null)
            {
                bpmnNodeVo.Property = new BpmnNodePropertysVo
                {
                    HrbpConfigType = bpmnNodeHrbpConf.HrbpConfigType
                };
            }

            return bpmnNodeVo;
        }


        public override void EditBpmnNode(BpmnNodeVo bpmnNodeVo)
        {
            var bpmnNodePropertysVo = bpmnNodeVo.Property ?? new BpmnNodePropertysVo();

            var bpmnNodeHrbpConf = new BpmnNodeHrbpConfig
            {
                BpmnNodeId = bpmnNodeVo.Id,
                HrbpConfigType = bpmnNodePropertysVo.HrbpConfigType,
                CreatorTime = DateTime.Now,
                //CreatorUserId = SecurityUtils.GetLogInEmpName(),
                LastModifyTime = DateTime.Now,
                //LastModifyUserId = SecurityUtils.GetLogInEmpName()
            };

            _bpmnNodeHrbpConfigService.Insert(bpmnNodeHrbpConf);
        }

        public override void SetSupportBusinessObjects()
        {
            ((IAdaptorService)this).AddSupportBusinessObjects(BpmnNodeAdpConfigEnum.ADP_CONF_NODE_PROPERTY_HRBP);
        }
    }
}
